<template>
  <el-form
    ref="formRef"
    class="search-container"
    label-width="120px"
    :model="formData"
    inline
    @submit.prevent
  >
    <el-row class="search-row">
      <template v-for="item in forms" :key="item.prop">
        <el-col :span="8">
          <el-form-item
            v-bind="item"
            :label="item.label ? item.label + ':' : ''"
            class="search-form-item"
          >
            <el-input
              v-if="item.formType === 'INPUT'"
              v-model="formData[item.prop]"
              clearable
              v-bind="item"
              style="width: 100%"
              @keyup.enter="handleQuery()"
            />
            <el-select
              v-else-if="item.formType === 'SELECT'"
              v-model="formData[item.prop]"
              clearable
              v-bind="item"
              style="width: 100%"
              @change="item.change"
            >
              <el-option
                v-for="dict in item.option"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
            <el-date-picker
              v-else-if="item.formType === 'DATE_PICKER'"
              v-model="formData[item.prop]"
              v-bind="item"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </template>
      <el-col v-if="forms.length < 3" class="search-btns" :span="8">
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-col>
    </el-row>
    <div v-if="forms.length >= 3" class="search-btns">
      <el-button type="primary" @click="handleQuery">搜索</el-button>
      <el-button @click="resetQuery">重置</el-button>
    </div>
  </el-form>
</template>

<script lang="ts" setup>
import { onMounted, type PropType, ref, watch } from "vue";
const emit = defineEmits(["query", "reset"]);
interface TabItem {
  label: string;
  default: string;
  prop: string;
  formType: string;
  change: null;
  option: [{ value: ""; label: "" }];
}
const props = defineProps({
  forms: {
    type: Array as PropType<TabItem[]>,
    default: () => []
  },
  immediate: {
    type: Boolean,
    default: true
  }
});
const formRef = ref(null);
const formData = ref({});

// 获取formData
const getFormData = () => {
  return formData.value;
};

// 查询按钮
const handleQuery = () => {
  emit("query", getFormData());
};

const resetQuery = () => {
  formRef.value.resetFields();
  const forms = props.forms;
  forms.map(item => {
    if (item.default) {
      formData.value[item.prop] = item.default;
    }
  });
  emit("reset", getFormData());
};

// watch监听forms处理
const handleForms = () => {
  const forms = props.forms;
  const obj = {};
  forms.map(item => {
    obj[item.prop] = item.default;
  });
  formData.value = obj;
};

onMounted(() => {
  if (props.immediate) {
    handleQuery();
  }
});

watch(props.forms, () => handleForms(), {
  deep: true
});

// 子组件暴漏给父组件ref-dom可调用
defineExpose({
  getFormData,
  formRef
});
</script>

<style lang="scss" scoped>
.search-container {
  display: flex;
  width: 100%;
  .search-row {
    flex: 1;
  }
  .search-form-item {
    width: calc(100% - 16px);
  }
  :deep(.el-form-item) {
    margin-bottom: 16px;
  }
  :deep(.el-form-item__label) {
    font-weight: 400;
    padding-right: 8px;
  }
}
</style>
